package com.zlj.accountant.app.common;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;

public 	abstract class JAsyncTask<Params,R extends JAsyncTask.Result>extends AsyncTask<Params, Void, R> {
	
	public static class Result {
		private Throwable e;

		public Throwable getE() {
			return e;
		}

		public void setE(Throwable e) {
			this.e = e;
		}
		
	}
	
	public static final int asyncDialog = -3238109;
	
	protected Activity context;
	
	int taskCount;
	
	public JAsyncTask(Activity context) {
		this.context = context;
	}
	
	@Override
	protected void onPreExecute() {
		synchronized (this) {
			taskCount++;
		}
		if(taskCount==1)context.showDialog(asyncDialog);
	}
	
	@Override
	protected void onPostExecute(R result) {
		if(result.getE()!=null){
//			Log.e(result.getClass().getName(), result.getE().getMessage());
			throw new RuntimeException(result.getE());
//			if(result.getE() instanceof ExcutefailedException){
//				UIUtil.catchExcutefailedException(context, (ExcutefailedException)result.getE());				
//			}
		}else{				
			postExecute(result);
		}
		synchronized (this) {
			taskCount--;
		}
		if(taskCount==0)context.removeDialog(asyncDialog);
	}
	
	protected abstract void postExecute(R result);
}
